﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace LightControls
{
    [TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
    [TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "Pressed", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
    public class LcImageButton : LcButton
    {
        protected Image buttonImage;
        protected TextBlock buttonTextBlock;
        
        public LcImageButton()
        {
            this.DefaultStyleKey = typeof(LcImageButton);
            this.Orientation = Orientation.Horizontal;
        }

        private void updateContents()
        {
            if (this.buttonTextBlock != null)
            {
                if (string.IsNullOrEmpty(this.Text))
                {
                    buttonTextBlock.Visibility = Visibility.Collapsed;
                }
                else
                {
                    buttonTextBlock.Visibility = Visibility.Visible;
                }
            }
            if (this.buttonImage != null)
            {
                if (this.ImageSource == null)
                {
                    buttonImage.Visibility = Visibility.Collapsed;
                }
                else
                {
                    buttonImage.Visibility = Visibility.Visible;
                }
            }
        }

        #region Properties
        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(LcImageButton), null);
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LcImageButton), null);
        public static readonly DependencyProperty ImageStretchProperty = DependencyProperty.Register("ImageStretch", typeof(Stretch), typeof(LcImageButton), null);
        public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(LcImageButton), null);

        public ImageSource ImageSource
        {
            set
            {
                SetValue(ImageSourceProperty, value);
                updateContents();
            }

            get
            {
                return GetValue(ImageSourceProperty) as ImageSource;

            }
        }

        public Stretch ImageStretch
        {
            set
            {
                SetValue(ImageStretchProperty, value);
            }

            get
            {
                return (Stretch)GetValue(ImageStretchProperty);
            }
        }

        public string Text
        {
            set
            {
                SetValue(TextProperty, value);
                updateContents();
            }

            get
            {
                return GetValue(TextProperty) as string;
            }
        }

        public Orientation Orientation
        {
            set
            {
                SetValue(OrientationProperty, value);
            }

            get
            {
                return (Orientation)GetValue(OrientationProperty);
            }
        }

        #endregion

        #region Events

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.buttonImage = base.GetTemplateChild("buttonImage") as Image;
            this.buttonTextBlock = base.GetTemplateChild("buttonTextBlock") as TextBlock;
        }
        #endregion

    }
}
